from django.test import TestCase
from django.utils.six import BytesIO
from rest_framework.parsers import JSONParser

from app.models import FMUser


class LoginTest(TestCase):
    def setUp(self):
        encrypted_password = 'adminadmin'  # make_password('adminadmin')
        admin_user = FMUser.objects.create_superuser(username='admin',
                                                     email='admin@example.com',
                                                     password=encrypted_password)
        admin_user.save()

    def tearDown(self):
        pass

    def test_login(self):
        response = self.client.post('/api/login/',
                                    data='{"username": "admin", "password": "adminadmin"}',
                                    content_type='application/json',
                                    accept_media_type='application/json')
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))

        response2 = self.client.post('/api/login/',
                                     data='{"username": "admin", "password": "adminadmin"}',
                                     content_type='application/json',
                                     accept_media_type='application/json')
        self.assertEqual(response2.status_code, 200)
        data = JSONParser().parse(BytesIO(response2.content))
